package coc

import (
	"fmt"
	"strings"
	"trpg-message/commands/trpg"
	"trpg-message/entity"
)

type Sc struct {
}

func (sc Sc) Handle(message entity.Message, argStr string) string {
	splitArg := strings.Split(argStr, " ")
	if len(splitArg) != 1 {
		return "错误的参数，请参考help"
	}
	attributes := roomDao.GetRoomBindCharacter(message.RoomId, message.Creator)
	if attributes == nil {
		return "未设置角色san值，请设置"
	}
	sanSet, ok1 := attributes["san"]
	if ok1 == false {
		return "未设置角色san值，请设置"
	}
	splitMinus := strings.Split(splitArg[0], "/")
	if len(splitMinus) != 2 {
		return "错误的参数，请参考help重新输入"
	}
	checkResult, rand := trpg.GetCheckResult(sanSet, message.RoomId)
	result := fmt.Sprintf("「%v」进行sc检定：1d100=%v/%v", message.CreatorName, rand, sanSet)
	minusSanStr := "0"
	minusSan := 0
	switch checkResult {
	case 0:
		minusSanStr, minusSan = trpg.GetLongRand(splitMinus[0], 0)
		result += "大成功！san值减少：" + minusSanStr
	case 1:
		minusSanStr, minusSan = trpg.GetLongRand(splitMinus[0], 1)
		result += "极难成功！san值减少：" + minusSanStr
	case 2:
		minusSanStr, minusSan = trpg.GetLongRand(splitMinus[0], 1)
		result += "困难成功！san值减少：" + minusSanStr
	case 3:
		minusSanStr, minusSan = trpg.GetLongRand(splitMinus[0], 1)
		result += "成功！san值减少：" + minusSanStr
	case 4:
		minusSanStr, minusSan = trpg.GetLongRand(splitMinus[1], 1)
		result += "失败！san值减少：" + minusSanStr
	case 5:
		minusSanStr, minusSan = trpg.GetLongRand(splitMinus[1], 2)
		result += "大失败！san值减少：" + minusSanStr
	}
	attributes["san"] = sanSet - int32(minusSan)
	roomDao.SetCharacterAttr(message.RoomId, message.Creator, attributes)
	return result
}
